/* Sara v1.0.0 Development */
/* Powered By (China)Skyogo Studio */
/* ©2018 Skyogo工作室️版权所有 */

//使用此方法以在JS里使用sara代码
function saraUse(content){
	saraSplit(content,"JavaScript Function");
}
//使用此方法获取sara的版本
function saraVersion(){
	return "1.0.0";
}
//调用此方法来获取一个sara文件
function sara(url){
	var xmlhttp;
    if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
    }else{
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
				saraSplit(xmlhttp.responseText,url);
			}
	    }
	if(url.substr(-4,4) === ".sar"){
		xmlhttp.open("POST",url,true);
        xmlhttp.send();
	}else{
		console.error("Sara: missing \".sar\"");
	}
}
//系统拆分方法，用户禁止调用
function saraSplit(saraContent,url){
	saraEndCondition = "";
	saraIsStop = false;
	console.groupCollapsed("Sara: "+url);
	console.time("Sara: Run file use ");  
	console.log("Sara: Starting to Run the file: "+url);
	saraContent = saraContent.replace(/print/g,"print;");
	saraContent = saraContent.replace(/tip/g,"tip;");
	saraContent = saraContent.replace(/do/g,"do;");
	saraContent = saraContent.replace(/id/g,"id;");
	saraContent = saraContent.replace(/let/g,"let;");
	saraContent = saraContent.replace(/final/g,"final;");
	saraContent = saraContent.replace(/global/g,"global;");
	saraContent = saraContent.replace(/\(/g,"(;");
	saraContent = saraContent.replace(/\)/g,";);");
	saraContent = saraContent.replace(/\{/g,"{;");
	saraContent = saraContent.replace(/\}/g,";};");
	saraContent = saraContent.replace(/,/g,";");
	saraContent = saraContent.replace(/\./g,";");
	saraContent = saraContent.replace(/&sign2250/g,"(");
	saraContent = saraContent.replace(/&sign2251/g,")");
	saraContent = saraContent.replace(/&sign2252/g,"{");
	saraContent = saraContent.replace(/&sign2253/g,"}");
	saraContent = saraContent.replace(/&sign2254/g," ");
	saraContent = saraContent.replace(/&sign2256/g,",");
	saraContent = saraContent.replace(/&sign2257/g,".");
	//saraContent = saraContent.replace(/<\/?.+?>/g,"");
	saraContent = saraContent.replace(/[\r\n]/g, "");
	saraContent = saraContent.replace(/&englisha/g,"a");
	saraContent = saraContent.replace(/&englishb/g,"b");
	saraContent = saraContent.replace(/&englishc/g,"c");
	saraContent = saraContent.replace(/&englishd/g,"d");
	saraContent = saraContent.replace(/&englishe/g,"e");
	saraContent = saraContent.replace(/&englishf/g,"f");
	saraContent = saraContent.replace(/&englishg/g,"g");
	saraContent = saraContent.replace(/&englishh/g,"h");
	saraContent = saraContent.replace(/&englishi/g,"i");
	saraContent = saraContent.replace(/&englishj/g,"j");
	saraContent = saraContent.replace(/&englishk/g,"k");
	saraContent = saraContent.replace(/&englishl/g,"l");
	saraContent = saraContent.replace(/&englishm/g,"m");
	saraContent = saraContent.replace(/&englishn/g,"n");
    saraContent = saraContent.replace(/&englisho/g,"o");
	saraContent = saraContent.replace(/&englishp/g,"p");
	saraContent = saraContent.replace(/&englishq/g,"q");
	saraContent = saraContent.replace(/&englishr/g,"r");
	saraContent = saraContent.replace(/&englishs/g,"s");
	saraContent = saraContent.replace(/&englisht/g,"t");
	saraContent = saraContent.replace(/&englishu/g,"u");
	saraContent = saraContent.replace(/&englishv/g,"v");
	saraContent = saraContent.replace(/&englishw/g,"w");
	saraContent = saraContent.replace(/&englishx/g,"x");
	saraContent = saraContent.replace(/&englishy/g,"y");
	saraContent = saraContent.replace(/&englishz/g,"z");
	saraRead(saraContent.split(";"));
}
//系统编译方法，用户禁止调用
function saraRead(saraContentArr){
	//初始化编译
	for(var i=0;i<saraContentArr.length;i++){
		saraContentArr[i] = saraContentArr[i].toString().replace(/&sign2255/g,";");
	}
	//正式编译
	var saraIsJsOpen = false;
	var saraJSCode = "";
	for(var i=0;i<saraContentArr.length;i++){
		if(saraContentArr[i].toString().substr(0,2) === "//"||saraContentArr[i].toString() === ""||saraContentArr[i].toString() === " "){
			continue;
		}else if(saraContentArr[i].toString().substr(0,2) === "/*"){
			saraIsStop = true;
		}else if(saraContentArr[i].toString().substr(0,2) === "*/"){
			saraIsStop = false;
		}else if(saraIsStop){
			continue;
		}else if(saraContentArr[i] === "print"){
			if(saraContentArr[i+1].toString().substr(0,2) != "ln"){
				eval("document.getElementsByTagName('body')[0].innerHTML += "+saraContentArr[i+2]);
			    i+=1;
			    continue;
			}else{
				eval("document.getElementsByTagName('body')[0].innerHTML += "+saraContentArr[i+2]);
			    document.getElementsByTagName('body')[0].innerHTML += "<br>";
			    i+=1;
			    continue;
			}
		}else if(saraContentArr[i] === "id"){
			eval("var "+saraContentArr[i+1]);
			i+=1;
			continue;
		}else if(saraContentArr[i] === "let"){
			eval("let "+saraContentArr[i+1]);
			i+=1;
			continue;
		}else if(saraContentArr[i] === "final"){
			eval("const "+saraContentArr[i+1]);
			i+=1;
			continue;
		}else if(saraContentArr[i] === "js"){
			saraIsJsOpen = true;
			saraContentArr[i]
			continue;
		}else if(saraContentArr[i] === "-js"){
			saraIsJsOpen = false;
			eval(saraJSCode);
			saraJSCode = "";
			continue;
		}else if(saraContentArr[i] === "tip"){
			eval("alert("+saraContentArr[i+2]+")");
			i+=1;
			continue;
		}else if(saraContentArr[i].toString() === "do"){
			var saraFunName0 = saraContentArr[i+1].toString();
			if(saraFunName0.substr(-1,1)==="("){
				var saraFunName = saraFunName0.substr(0,saraFunName0.length-1);
				eval(saraFunName+"()");
			}else{
				eval(saraFunName0+"()");
			}
			i+=1;
			continue;
		}else if(saraContentArr[i].toString().substr(0,3) === "use"){
			var saraURL = saraContentArr[i+1];
			var xmlhttp;
			if (window.XMLHttpRequest){
		        xmlhttp=new XMLHttpRequest();
            }else{
		        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
	        xmlhttp.onreadystatechange=function(){
		        if (xmlhttp.readyState==4 && xmlhttp.status==200){
				    saraSplit(xmlhttp.responseText,saraURL);
			    }
	        }
			if(saraURL.substr(-5,4) === ".sar"){
				eval("xmlhttp.open('POST',"+saraURL+",true)");
                xmlhttp.send();
			}else{
				console.error("Sara: missing \".sar\"");
				saraEndCondition = "error";
				break;
			}
			i+=1;
			continue;
		}else if(saraContentArr[i].toString() === "global"){
			eval("window."+saraContentArr[i+1]);
			i+=1;
			continue;
		}else if(saraContentArr[i].toString() === "windo"&&saraContentArr[i+1].toString() === "w"){
			if(saraContentArr[i+2].toString() === "removeEle("){
				eval("document.querySelector("+saraContentArr[i+3].toString()+").remove()");
				i+=1;
				continue;
			}else if(saraContentArr[i+2].toString() === "empty("){
				eval("document.querySelector("+saraContentArr[i+3].toString()+").innerHTML = '';");
				i+=1;
				continue;
			}else if(saraContentArr[i+2].toString() === "setCss("){
				eval("var saraCssName = "+saraContentArr[i+4].toString());
				eval("document.querySelector("+saraContentArr[i+3].toString()+").style."+saraCssName+" = "+saraContentArr[i+5].toString());
				i+=3;
				continue;
			}else if(saraContentArr[i+2].toString() === "setAttr("){
				eval("document.querySelector("+saraContentArr[i+3].toString()+").setAttribute("+saraContentArr[i+4].toString()+","+saraContentArr[i+5].toString()+")");
				i+=3;
				continue;
			}else{
				console.error("Sara: unknown window.");
				saraEndCondition = "error";
				break;
			}
		}else if(saraContentArr[i].toString() === "#end"){
			saraEndCondition = "end";
			break;
		}
		if(saraIsJsOpen){
			saraJSCode+=saraContentArr[i];
		}
	}
	console.timeEnd("Sara: Run file use ");
	if(saraEndCondition === ""||saraEndCondition === null){
		console.log("Sara: Run file complete."); 
	}else if(saraEndCondition === "error"){
		console.log("Sara: Run file stopped, the file had error!"); 
	}else if(saraEndCondition === "end"){
		console.log("Sara: Run file complete, By the \"#end\""); 
	}else{
		console.log("Sara: unknown file end."); 
	}
    console.groupEnd();
}